package services

import (
	"myapi/internal/models"
	"myapi/internal/repositories"
)

type ArticleService struct {
	repo *repositories.ArticleRepository
}

func NewArticleService(repo *repositories.ArticleRepository) *ArticleService {
	return &ArticleService{repo: repo}
}

func (s *ArticleService) Create(article *models.Article) error {
	return s.repo.Create(article)
}

func (s *ArticleService) GetByID(id uint) (*models.Article, error) {
	return s.repo.Detail(id)
}

func (s *ArticleService) GetAll(page, pageSize int) ([]*models.Article, error) {
	if page < 1 {
		page = 1
	}
	if pageSize < 1 || pageSize > 100 {
		pageSize = 10
	}
	return s.repo.GetAll(page, pageSize)
}

func (s *ArticleService) Update(id uint, articleData *models.Article) error {
	article, err := s.repo.Detail(id)
	if err != nil {
		return err
	}
	// 更新字段
	article.Title = articleData.Title
	article.Content = articleData.Title
	return s.repo.Update(article)
}

func (s *ArticleService) Delete(id uint) error {
	return s.repo.Delete(id)
}
